home *** CD-ROM | disk | FTP | other *** search
/ Ham Radio 2000 / Ham Radio 2000.iso / ham2000 / tcp_ip / gp / 7plus.c < prev    next >
C/C++ Source or Header  |  1991-12-31  |  13KB  |  374 lines

  1. /* Version */
  2. #define _7P_VMAJOR 1
  3. #define _7P_VMINOR 63
  4. #define VERSION "1.63"
  5. #define DATE "911231"
  6.  
  7. /*********************************************************************
  8. *** 7PLUS ASCII- Encoder/Decoder, Axel Bauda, DG1BBQ @DB0CL.DEU.EU  ***
  9. **********************************************************************
  10. ***
  11. *** Compile:
  12. *** --------
  13. ***
  14. *** -------
  15. *** | IBM |  : SMALL memory model
  16. *** -------
  17. ***            Turbo C++ and Turbo C 2.0:
  18. ***            MAKE -FTC_MAKE
  19. ***
  20. ***      also: Turbo C++:
  21. ***            Compile in IDE using 7PLUS.PRJ
  22. ***
  23. ***      or  : Turbo C 2.0:
  24. ***            Rename TC2.PRJ to 7PLUS.PRJ and compile in IDE
  25. ***
  26. *** ---------
  27. *** | ATARI |  Turbo C 2.0 : Use 7PLST.PRJ
  28. *** ---------
  29. *** --------
  30. *** | UNIX |   support by Torsten H. Bischoff, DF6NL @ DB0BOX
  31. *** --------
  32. *** Supported systems are:
  33. ***   Interactive UNIX 386
  34. ***   SCO XENIX 386
  35. ***   VAX BSD 4.3/ Ultrix 4.1
  36. ***
  37. *** Compile: make -fu_make
  38. ***
  39. *** -------
  40. *** |AMIGA|    Manx Aztec C 5.1b/5.2:  make -fa_make
  41. *** -------
  42. ***
  43. *** All systems:
  44. *** Signed or unsigned char per default is: don't care.
  45. *** No floating point lib required.
  46. ***
  47. *** TABSIZE when editing: 2; don't insert real TABs (^I), use spaces instead.
  48. ***
  49. *** When porting or modifying this source, make SURE it can still be compiled
  50. *** on all systems! Do this by using #ifdef directives! Please let me know
  51. *** about the modifications or portations, so I can include them in the origi-
  52. *** nal 7PLUS.
  53. ***
  54. **********************************************************************
  55. *** 7PLUS ASCII- Encoder/Decoder, Axel Bauda, DG1BBQ @DB0CL.DEU.EU  ***
  56. **********************************************************************
  57. ***
  58. *** File converter for transfer of arbitrary binary data
  59. *** via store & forward.
  60. ***
  61. *** 7PLUS is HAMWARE. No commercial use. Pass on only in it's entirety!
  62. *** There is no warranty for the proper functioning. Use at own risk.
  63. ***
  64. *** Tnx to:
  65. *** DB7YAH, DG3YFU and DL6YAV for testing and tips regarding ATARI ST.
  66. *** DB5ZP for his efforts concerning the AMIGA implementation.
  67. *** DF6NL for adding UNIX-compatibility.
  68. ***
  69.  */
  70.  
  71. #include "7plus.h"
  72.  
  73. /** globals **/
  74. uint    crctab[256];
  75. byte    decode[256];
  76. byte    code  [216];
  77. byte    extended = INDICATE;
  78. size_t  buflen;
  79. char    _drive[MAXDRIVE], _dir[MAXDIR], _file[MAXFILE], _ext[MAXEXT];
  80. char    spaces[] = "                                                   ";
  81. char    *endstr;
  82. char    tmpfil[]  = "7plus.tmp";
  83. char    delimit[] = "\n";
  84. char    cant[] = "\007\n'%s': Can't open. Break.\n";
  85. char    miss[] = "\n\t\tLine %03d missing/corrupted.";
  86. char    notsame[] = "\007Filesize in %s differs from the original file!\nBreak.\n";
  87. int     noquery = 0;
  88. int     fls     = 0;
  89.  
  90. #ifdef __TOS__
  91.   int   nowait  = 0;
  92. #endif
  93.  
  94. char logon_ctrl[] =
  95. #ifdef __MSDOS__
  96. "\n╔════════════════════════════════════════════╗\n║%s║\n║%s║\n╚══════════\
  97. ══════════════════════════════════╝\n";
  98. #else
  99.  #ifdef __TOS__
  100. "\033p\033v\n%s\n%s\n\033q\n";
  101.  #else
  102.   #ifdef _AMIGA_
  103. "\n\033[3m%s\n%s\033[0m\n\n";
  104.   #else    /* neither __MSDOS__ or __TOS__ or _AMIGA_ */
  105. "\n[]------------------------------------------[]\n|%s|\n|%s|\n[]---------\
  106. ---------------------------------[]\n";
  107.   #endif   /* ifdef _AMIGA_   */
  108.  #endif   /* ifdef __TOS__   */
  109. #endif   /* ifdef __MSDOS__ */
  110.  
  111. char *logon[] = { " 7PLUS - file converter for store & forward ",
  112.                   " version "VERSION" ("DATE"), DG1BBQ@DB0CL.DEU.EU " };
  113.  
  114. char *help[] = {
  115. #ifdef _AMIGA_
  116. "\n\033[1mExamples:\n",
  117. "7plus <FILE.7pl>     Decode <FILE.7pl>. create <FILE.err>, if errors detected\n",
  118. "7plus <FILE.p01>     Recombine & decode.                    \"\n",
  119. "7plus <FILE>         Automatically correct and decode.\n",
  120. "                     If a file named <FILE> exists, encode it.\n",
  121. "7plus <FILE> -s      Encode & split <FILE> (default: 140 lines/file, 10K).\n",
  122. "             -s 30   30 lines/file (max 512 lines/file).\n",
  123. "             -sp 3   3 parts of roughly equal size (max 255 parts).\n",
  124. "7plus <FILE>         Encode (automatically split if more than 512 lines).\n",
  125. "7plus <FILE.err> dh0:/pr/ Create correction file. look for <FILE> in dh0:/pr/.\n",
  126. "7plus <FILE.cor>     Use <FILE.cor> to correct <FILE.pXX>/<FILE.7pl>.\n",
  127. "7plus <LOGFILE> -x <TEXT> Extract 7plus-files from <LOGFILE>. Only extract a\n",
  128. "                     file, if it's name contains <TEXT>. Omit <TEXT> to extract\n",
  129. "                     files in <LOGFILE>.\n",
  130. "7plus <FILE.err> <FILE2.err> -j  Add contents of error report <FILE2err> to\n",
  131. "                     error report <FILE.err>.\n",
  132. "7plus <FILE.err> -j  Add contents of error reports <FILE.eXX> to error\n",
  133. "                     report <FILE.err> (multiple join).\n",
  134. "Other Options:\n",
  135. "-p           Use Packet line separator CR for encoded files. Should be\n",
  136. "             used, when uploading files to the BBS in binary mode!\n",
  137. "-t /ex       Append string '/ex' to encoded files (BBS file termination).\n",
  138. "-y           Assume YES on all queries.\n",
  139. "-#           Create 7PLUS.FLS when encoding. See Manual.\n",
  140. "\033[0m\n\n",
  141. #else
  142. "Examples:\n",
  143. "7plus test.7pl         Decode test.7pl. create test.err, if errors detected.\n",
  144. "7plus test.p01         Recombine & decode.              \"\n",
  145. "7plus test             Automatically correct and decode.\n",
  146. "                       If a file named test exists, encode it.\n",
  147. "7plus test.txt -s      Encode & split test.txt (default: 140 lines/file, 10K).\n",
  148. "               -s 30   30 lines/file (max 512 lines/file).\n",
  149. "               -sp 3   3 parts of roughly equal size (max 255 parts).\n",
  150. "7plus test.txt         Encode (automatically split if more than 512 lines).\n",
  151. "7plus test.err c:\\pr\\  Create correction file. look for test.txt in c:\\pr\\.\n",
  152. "7plus test.cor         Use test.cor to correct test.pXX/test.7pl.\n",
  153. "7plus logfile -x text  Extract 7plus-files from logfile. Only extract a file\n",
  154. "                       if it's name contains 'text'. Omit 'text' to extract\n",
  155. "                       all files in logfile.\n",
  156. "7plus test.err test2.err -j  Add contents of error report test2.err to\n",
  157. "                       error report test.err.\n",
  158. "7plus test.err -j      Add contents of error reports test.eXX to error\n",
  159. "                       report test.err (multiple join).\n",
  160. "Other Options:\n",
  161. #ifdef __TOS__
  162. "-n           Don't wait for a keystroke at termination.\n",
  163. #endif
  164. "-p           Use Packet line separator CR for encoded files. Should be\n",
  165. "             used, when uploading files to the BBS in binary mode!\n",
  166. "-t /ex       Append string '/ex' to encoded files (BBS file termination).\n",
  167. "-y           Assume YES on all queries.\n",
  168. "-#           Create 7PLUS.FLS when encoding. See Manual.\n",
  169. #endif
  170. NULL
  171. };
  172.  
  173.  
  174. /* Depending on the system, it may be nessesary to prompt the user for a
  175.    keystroke, before terminating, because user wouldn't be able to read
  176.    the outputs to the screen, when the window closes at termination.
  177.    However, the '-n' option overrides this. */
  178. int main (int argc, char **argv)
  179. {
  180. #ifdef __TOS__
  181.   int ret;
  182.   
  183.   ret = go_at_it (argc, argv);
  184.   if ((!nowait || noquery) && !system(NULL))
  185.   {
  186.     printf("\n\033p Hit any key \033q");
  187.     getch();
  188.   }
  189.   return (ret);
  190. #else
  191.   return (go_at_it (argc, argv));
  192. #endif
  193. }
  194.  
  195. /* This is the real main() */
  196. int go_at_it (int argc, char **argv)
  197. {
  198.   char *p, *q, *r, *s;
  199.   int  i, extract, join;
  200.   long blocksize;
  201.  
  202.   i = extract = join = 0;
  203.   p = q = r = s = endstr = NULL;
  204.   blocksize = 0L;
  205.  
  206.   printf (logon_ctrl, logon[0], logon[1]);
  207.  
  208.   while (++i<argc)
  209.   {
  210.     if (*argv[i] != '-')
  211.     {
  212.       if (!p)
  213.       {
  214.         p = argv[i];  /* Name of file to de/encode */
  215.         continue;
  216.       }
  217.       if (!r)
  218.       {
  219.         r = argv[i];  /* Searchpath for non-coded file. Needed for */
  220.         continue;     /* generating correction file */
  221.       }
  222.     }
  223.  
  224.     if (!stricmp (argv[i], "-S")) /* Split option */
  225.     {
  226.       i++;
  227.       if (i == argc)
  228.       {
  229.         blocksize = 140 * 62;  /* No parameter, set default blocksize
  230.                                   to 140 lines */
  231.         i--;
  232.       }
  233.       else
  234.         blocksize = atol (argv[i]) * 62; /* Set blocksize to parameter */
  235.     }
  236.  
  237.     if (!stricmp (argv[i], "-SP")) /* Split in equal parts */
  238.     {
  239.       i++;
  240.       if (i == argc)
  241.       {
  242.         blocksize = 0; /* No parameter, no user defined split */
  243.         i--;
  244.       }
  245.       else
  246.         blocksize = 50000L + atol (argv[i]); /* Number of parts to encode */
  247.     }
  248.  
  249.     if (!stricmp (argv[i], "-T")) /* Define BBSs termination string */
  250.     {
  251.       i++;
  252.       if (i == argc)
  253.         i--;
  254.       else
  255.       {
  256.         endstr = malloc ((int) strlen (argv[i]) +1);
  257.         strcpy (endstr, argv[i]);
  258.       }
  259.     }
  260.  
  261.     if (!stricmp (argv[i], "-#")) /* Create 7PLUS.FLS. Contents e.g.:     */
  262.       fls = 1;                    /* 10 TEST */
  263.                                   /* for TEST.EXE encoded into 10 parts   */
  264.  
  265.     if (!stricmp (argv[i], "-J")) /* Join two error reports */
  266.       join = 1;
  267.  
  268.     #ifdef __TOS__
  269.     if (!stricmp (argv[i], "-N")) /* Don't wait for a key at termination  */
  270.       nowait = 1;
  271.     #endif
  272.  
  273.     if (!stricmp (argv[i], "-P")) /* Write encoded files in Packet format */
  274.       sprintf (delimit, "\r");    /* for direct binary upload. */
  275.  
  276.     if (!stricmp (argv[i], "-X")) /* Extract 7plus-files from log-file    */
  277.       extract = 1;
  278.  
  279.     if (!stricmp (argv[i], "-VMAJOR")) /* Return version number +100      */
  280.       return (_7P_VMAJOR +100);
  281.  
  282.     if (!stricmp (argv[i], "-VMINOR")) /* Return subversion number + 100  */
  283.       return (_7P_VMINOR+100);
  284.  
  285.     if (!stricmp (argv[i], "-Y")) /* Always assume YES on queries.*/
  286.       noquery = 1;
  287.  
  288.   }
  289.   if (!p ) /* No File specified, show help */
  290.   {
  291.     i = 0;
  292.     while (help[i])
  293.       printf ("%s", help[i++]);
  294.     return (0);
  295.   }
  296.  
  297.   /* Note: I used to check if there's enough memory by calling coreleft() or
  298.      _memmax() or sizmem(), but the Aztec compiler doesn't have any such
  299.      function. At least i couldn't find it. Anyway, this is more portable.*/
  300.   if ((s = malloc (32768U)) == NULL)
  301.   {
  302.      printf ("\007Argh error: Not enough memory present! Can't continue.....\n");
  303.      exit (12);
  304.   }
  305.   free (s);
  306.  
  307.   buflen = 16384;
  308.  
  309.   init_crctab (); /* Initialize table for CRC-calculation */
  310.   init_decodetab (); /* decoding-table */
  311.   init_codetab   (); /* encoding-table */
  312.  
  313.   if (extract)
  314.     if (p)
  315.       return (extract_files (p, r));
  316.     else
  317.     {
  318.       printf ("\007File to extract from not specified. Break.\n");
  319.       return (6);
  320.     }
  321.   if (join)
  322.     return (join_control (p, r));
  323.  
  324.   /* Does the filename contain an extension? */
  325.   if ((q = strrchr (p, '.')) != NULL)
  326.   {
  327.     /* Call decode_file() if ext ist 7PL, P01 or COR, else encode_file() */
  328.     if (!strnicmp (".7pl", q, 4) ||
  329.         !strnicmp (".p01", q, 4) ||
  330.         !strnicmp (".cor", q, 4))
  331.       return (control_decode (p));
  332.     return (encode_file (p, blocksize, r));
  333.   }
  334.   else
  335.   {
  336.     if (!test_exist (p)) /* no EXT, but file exists on disk, then encode */
  337.       return (encode_file (p, blocksize, r));
  338.     else
  339.       return (control_decode (p));
  340.   }
  341. }
  342.  
  343. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*
  344.  *+ Possible return codes:                                                 +*
  345.  *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*
  346.  
  347.   0 No errors detected.
  348.   1 Write error.
  349.   2 File not found.
  350.   3 7PLUS header not found.
  351.   4 File does not contain expected part.
  352.   5 7PLUS header corrrupted.
  353.   6 No filename for extracting defined.
  354.   7 invalid error report / correction file.
  355.   8 Max number of parts exceeded.
  356.   9 Bit 8 stripped.
  357.  10 User break in test_file();
  358.  11 Error report generated.
  359.  12 Only one or no error report to join
  360.  13 join: error reports do not relate to the same original file
  361.  14 Couldn't write 7plus.fls
  362.  15 Filesize of original file and the size reported in err/cor-file not equal
  363.  16 Correction not successful
  364.  
  365.  1XX If invoked with '-VMAJOR' option, 7PLUS returns version number.
  366.      e.g.: 101  <--- v1.5
  367.          : 108  <--- v8.8 ... hihi
  368.  
  369.      Invoked with '-VMINOR':
  370.      e.g.: 150  <--- v1.5
  371.          : 180  <--- v8.8
  372.  
  373.  *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  374.